﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Grain.Crm.Common;

namespace Grain.Crm.ExcelImporter.Serializer
{
    public class DataSerializer<T>
    {
        public DataSerializer()
        {
            serializer = new XmlSerializer(typeof(T));
        }

        public void Serialize(string filename, T objToSerialize)
        {
            using (var stream = new FileStream(Path.Combine(Helper.AssemblyDirectory, filename), FileMode.Create))
            {
                serializer.Serialize(stream, objToSerialize);
            }
        }

        public T Deserialize(string filename)
        {
            using (var stream = new FileStream(Path.Combine(Helper.AssemblyDirectory, filename), FileMode.Open))
            {
                return (T) serializer.Deserialize(stream);
            }
            
        }

        private readonly XmlSerializer serializer;
    }
}
